package project.order.internal;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import project.order.Order;
import project.order.OrderService;
import project.order.work.SendArticleThread;

public class OrderServiceImpl extends HibernateDaoSupport implements OrderService {

	
	@Override
    public void update(Order entity) {
        this.getHibernateTemplate().update(entity);

    }
	@Override
    public Order getOrder(Serializable id) {
        StringBuffer queryString = new StringBuffer(" FROM Order where id=?");
        List<Order> list = this.getHibernateTemplate().find(queryString.toString(), id);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
    @Override
    public void save(Order order) {
        order.setCreateTime(new Date());
        this.getHibernateTemplate().save(order);
    }

    @Override
    public void handle(String out_trade_no) {
        Order order = this.getHibernateTemplate().get(Order.class, out_trade_no);
        if (order != null) {
            order.setPayment(1);
            this.getHibernateTemplate().update(order);
        }else{
        	order.setPayment(0);
            this.getHibernateTemplate().update(order);
        }

        /**
         * 发送消息
         */
        SendArticleThread sendArticleThread = new SendArticleThread(order.getOpenId(), "您的订单已付款成功。");
        sendArticleThread.start();

    }

}
